{
 "metadata": {
  "name": "",
  "signature": "sha256:3c2fba170ef016a73da20e55696451bd53579b6c1aab71d0806a6cfbc2131701"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "abs(-2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 1,
       "text": [
        "2"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "len([-2,-3])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 2,
       "text": [
        "2"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tabs = abs\n",
      "tabs = len\n",
      "tabs([-2,-3]) #set new function to abs, which make abs to become the fuction len"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 3,
       "text": [
        "2"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def add(x,y,f):\n",
      "    return f(x) + f(y)\n",
      "print add(-5,5,abs)\n",
      "print add(5,5,sqrt)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10\n",
        "4.472135955\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def is_odd(x):\n",
      "    return x%2==1\n",
      "filter(is_odd,[1,4,5,5,6])#return true or false , and false is filtered"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 3,
       "text": [
        "[1, 5, 5]"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#\u88c5\u9970\u5668\u7528\u4e8e\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u589e\u5f3a\u539f\u51fd\u6570\u7684\u529f\u80fd\n",
      "def f1(x):\n",
      "    return x*2\n",
      "def new_fn(f):#\u8fd9\u4e2a\u5c31\u662f\u7528\u9ad8\u9636\u51fd\u6570\u7528\u4e8e\u88c5\u9970\n",
      "    def fn(x):\n",
      "        print 'call'+f.__name__+'()'\n",
      "        return f(x)\n",
      "    return fn\n",
      "f1 = new_fn(f1)\n",
      "print f1(5) #\u76f8\u5f53\u4e8e\u5bf9f1\u8fdb\u884c\u4e86\u4fee\u6539"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "callf1()\n",
        "10\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def log(f):\n",
      "    def fn(*args, **kw):\n",
      "        print 'call ' + f.__name__ + '()...'\n",
      "        return f(*args, **kw)\n",
      "    return fn\n",
      "\n",
      "@log\n",
      "def add(x, y):\n",
      "    return x + y\n",
      "print add(1, 2)#\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u7136\u540e\u5728\u9700\u8981\u4f7f\u7528\u51fd\u6570\u524d\u9762\u6dfb\u52a0\u4e00\u4e2a@function_name\u5c31\u53ef\u4ee5\u4e86\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "call add()...\n",
        "3\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def log(prefix):\n",
      "    def log_decorator(f):\n",
      "        def wrapper(*args, **kw):\n",
      "            print '[%s] %s()...' % (prefix, f.__name__)\n",
      "            return f(*args, **kw)\n",
      "        return wrapper\n",
      "    return log_decorator\n",
      "\n",
      "@log('DEBUG')\n",
      "def test():\n",
      "    pass\n",
      "print test()#\u901a\u8fc7\u4e24\u5c42\u88c5\u9970\u5668\u5b8c\u6210\u5bf9\u88c5\u9970\u5668\u5e26\u51fd\u6570\u7684\u5f15\u5165\n",
      "print test.__name__"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "[DEBUG] test()...\n",
        "None\n",
        "wrapper\n"
       ]
      }
     ],
     "prompt_number": 61
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import functools#\u901a\u8fc7\u8fd9\u4e2a\u628a\u539f\u51fd\u6570\u81ea\u5df1\u7684\u4fe1\u606f\u4ee3\u5165\n",
      "def log(f):\n",
      "    @functools.wraps(f)\n",
      "    def wrapper(*args, **kw):\n",
      "        print 'call...'\n",
      "        return f(*args, **kw)\n",
      "    return wrapper\n",
      "@log\n",
      "def test():\n",
      "    pass\n",
      "print test()#\u901a\u8fc7\u4e24\u5c42\u88c5\u9970\u5668\u5b8c\u6210\u5bf9\u88c5\u9970\u5668\u5e26\u51fd\u6570\u7684\u5f15\u5165\n",
      "print test.__name__"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "call...\n",
        "None\n",
        "test\n"
       ]
      }
     ],
     "prompt_number": 63
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#\u5bf9\u51fd\u6570\u7528\u65f6\u7684performance\u7684\u88c5\u9970\u5668\n",
      "import time\n",
      "def performance(f):\n",
      "\n",
      "    def fn(*args, **kw):\n",
      "\n",
      "        start = time.time()\n",
      "        a = f(*args, **kw)\n",
      "        end =time.time()\n",
      "        print 'call '+f.__name__+' using time :' + str(end-start)\n",
      "        return a\n",
      "    return fn\n",
      "@performance\n",
      "def add(x, y):\n",
      "    for i in range(1000000):\n",
      "        a=i\n",
      "    return x + y\n",
      "print add(1, 2)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "call add using time :0.0709998607635\n",
        "3\n",
        "fn\n"
       ]
      }
     ],
     "prompt_number": 60
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import log\n",
      "from logging import log as logger   # logging\u7684log\u73b0\u5728\u53d8\u6210\u4e86logger\n",
      "print log(10)   # \u8c03\u7528\u7684\u662fmath\u7684log\n",
      "logger(10, 'import from logging')   # \u8c03\u7528\u7684\u662flogging\u7684log,\u8fd9\u6837\u89e3\u51b3\u4e86\u540c\u540d\u7684\u95ee\u9898"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2.30258509299\n"
       ]
      }
     ],
     "prompt_number": 64
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Person(object):\n",
      "    count = 0\n",
      "    @classmethod\n",
      "    def how_many(cls):\n",
      "        return cls.count\n",
      "    def __init__(self, name):\n",
      "        self.name = name\n",
      "        Person.count = Person.count + 1\n",
      "\n",
      "print Person.how_many()\n",
      "p1 = Person('Bob')\n",
      "print Person.how_many()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0\n",
        "1\n"
       ]
      }
     ],
     "prompt_number": 65
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Person(object):\n",
      "    def __init__(self, name, gender):\n",
      "        self.name = name\n",
      "        self.gender = gender\n",
      "    def __str__(self):\n",
      "        return '(Person: %s, %s)' % (self.name, self.gender)\n",
      "    __repr__ = __str__\n",
      "p=Person('Bob',12)\n",
      "p"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 67,
       "text": [
        "(Person: Bob, 12)"
       ]
      }
     ],
     "prompt_number": 67
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Students(object):\n",
      "    def __init__(self, *args):\n",
      "        self.names = args\n",
      "    def __len__(self):\n",
      "        return len(self.names)\n",
      "\n",
      "ss = Students('1','2','3')\n",
      "len(ss)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 68,
       "text": [
        "3"
       ]
      }
     ],
     "prompt_number": 68
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Rational(object):\n",
      "    def __init__(self, p, q):\n",
      "        self.p = p\n",
      "        self.q = q\n",
      "    def __add__(self, r):\n",
      "        return Rational(self.p * r.q + self.q * r.p, self.q * r.q)\n",
      "    def __str__(self):\n",
      "        return '%s/%s' % (self.p, self.q)\n",
      "    __repr__ = __str__\n",
      "\n",
      "r1 = Rational(1, 3)\n",
      "r2 = Rational(1, 2)\n",
      "print r1 + r2\n",
      "#\u51cf\u6cd5\u8fd0\u7b97\uff1a__sub__\n",
      "#\u4e58\u6cd5\u8fd0\u7b97\uff1a__mul__\n",
      "#\u9664\u6cd5\u8fd0\u7b97\uff1a__div__"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5/6\n"
       ]
      }
     ],
     "prompt_number": 69
    }
   ],
   "metadata": {}
  }
 ]
}